// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import productInfo from './productInfo.js'
import { promises } from 'fs';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    spaceId: '',
    activeOption: 'baseInfo',
    activePageIndex: 0,
    //productInfo: productInfo,
    productInfo: {},
    productInfoModify: false,
    packages: { totalCount: 20 },
    packageLogs: { totalCount: 20 },
    slides: [],
    packageQueryParams: {
      start: 0,
      limit: 10,
      bq: '',
      orderby: 'and(created,orderby,desc)'
    },
    packageLogQueryParams: {
      start: 0,
      limit: 10,
      bq: '',
      orderby: 'and(created,orderby,desc)'
    },
    activeName: '/nav/baseInfo'
  },
  mutations: {
    setActiveName(state, name) {
      state.activeName = name;
    },
    setSpaceId(state, spaceId) {
      state.spaceId = spaceId;
    },
    changeActiveOption(state, activeOption) {
      state.activeOption = activeOption;
    },
    changeActivePageIndex(state, activePageIndex) {
      state.activePageIndex = activePageIndex;
    },
    addSlidePage(state, pageInfo) {
      state.productInfo[pageInfo.activeOption].pages.page.splice(pageInfo.activePageIndex + 1, 0, pageInfo.content);
      state.productInfoModify = true;
    },
    deleteSlidePage(state, pageInfo) {
      state.productInfo[pageInfo.activeOption].pages.page.splice(pageInfo.activePageIndex, 1);
      state.productInfoModify = true;
    },
    initSlides(state) {
      state.slides = [];
      for (var i = 1; i <= 24; i++) {
        var slideInfo = {
          name: "slidePage_" + i + ".png",
          url: "/magicflu/buildpackage/src/slidepng/slidePage_" + i + ".png?t=" + + new Date().getTime(),
          nsisUseName: "系统轮播图" + i + ".png"
        }
        state.slides.push(slideInfo);
      }
    },
    getProductInfo(state) {
      //let data = "<entry><id></id><productName>excel进销存</productName><productEdition>e</productEdition><version>1.0.0.2</version><navigationLogo></navigationLogo><aboutLogo></aboutLogo><deskTopIcon></deskTopIcon><workspace>00000000-0000-0000-0000-000000000000</workspace><installPage><![CDATA[<pages><page><pageName>首页</pageName><pageType>0</pageType><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage><license><name></name><url></url><nsisUseName></nsisUseName></license></page><page><pageName>轮播页1</pageName><pageType>1</pageType><stayTime>轮播时间</stayTime><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page><page><pageName>轮播页2</pageName><pageType>1</pageType><stayTime>轮播时间</stayTime><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page><page><pageName>数据库选择页</pageName><pageType>2</pageType><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page><page><pageName>安装完成页</pageName><pageType>2</pageType><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page></pages>]]></installPage><upgradePage><![CDATA[<pages><page><pageName>升级提示页</pageName><pageType>2</pageType><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage><license><name></name><url></url><nsisUseName></nsisUseName></license></page><page><pageName>轮播页1</pageName><pageType>1</pageType><stayTime>轮播时间</stayTime><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page><page><pageName>轮播页2</pageName><pageType>1</pageType><stayTime>轮播时间</stayTime><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page><page><pageName>升级完成页</pageName><pageType>2</pageType><backgroundImage><name></name><url></url><nsisUseName></nsisUseName></backgroundImage></page></pages>]]></upgradePage></entry>";
      wait();
      axios({
        url: '/magicflu/service/s/json/' + state.spaceId + '/products/qcreate/feed?start=0&limit=-1',
        method: 'get',
        responseType: 'json',
      }).then(function (response) {
        state.productInfo = response.data.entry[0];
        jdiclose();
      }).catch(function (response) {
        jdiclose();
      });
    },
    getPackages(state) {
      wait();
      axios({
        url: '/magicflu/service/s/json/' + state.spaceId + '/packages/feed?start=' + state.packageQueryParams.start + '&limit=' + state.packageQueryParams.limit + '&bq=' + encodeURIComponent(state.packageQueryParams.bq + state.packageQueryParams.orderby),
        method: 'get',
        responseType: 'json'
      }).then(function (response) {
        state.packages = response.data;
        jdiclose();
      }).catch(function (response) {
        jdiclose();
      });
    },
    getPackageLogs(state) {
      wait();
      axios({
        url: '/magicflu/service/s/json/' + state.spaceId + '/packagelogs/feed?start=' + state.packageLogQueryParams.start + '&limit=' + state.packageLogQueryParams.limit + '&bq=' + encodeURIComponent(state.packageLogQueryParams.bq + state.packageLogQueryParams.orderby),
        method: 'get',
        responseType: 'json'
      }).then(function (response) {
        state.packageLogs = response.data;
        jdiclose();
      }).catch(function (response) {
        jdiclose();
      });
    },
    updatePackageQueryParams(state, params) {
      state.packageQueryParams = params;

    },
    updatePackageLogQueryParams(state, params) {
      state.packageLogQueryParams = params;
    },
    updateProductInfo(state, obj) {
      for (var key in obj) {
        Vue.prototype.$set(state.productInfo, key, obj[key]);
      }
      state.productInfoModify = true;
    },
    updateBaseInfoResource(state, obj) {
      for (var key in obj) {
        state.productInfo[key] = obj[key];
      }
      state.productInfoModify = true;
    },
    updateInstallOrUpgradePageResource(state, obj) {
      state.productInfo[state.activeOption].pages.page[state.activePageIndex].backgroundImage = obj;
      state.productInfoModify = true;
    },
    updateLicense(state, obj) {
      state.productInfo[state.activeOption].pages.page[state.activePageIndex].license = obj;
      state.productInfoModify = true;
    },
    updateStayTime(state, value) {
      state.productInfo[state.activeOption].pages.page[state.activePageIndex].stayTime = value;
      state.productInfoModify = true;
    },
    updatePageName(state, value) {
      state.productInfo[state.activeOption].pages.page[state.activePageIndex].pageName = value;
    },
    updatePackageOrLogList(state, index) {
      if (state.activeOption == 'packageManagement') {
        state.packages.entry.splice(index, 1);
      } else if (state.activeOption == 'packageLogs') {
        state.packageLogs.entry.splice(index, 1);
      }
    },
    updatePackageOrLogTotalcount(state, num) {
      if (state.activeOption == 'packageManagement') {
        state.packages.totalCount = state.packages.totalCount - 1;
      } else if (state.activeOption == 'packageLogs') {
        state.packageLogs.totalCount = state.packageLogs.totalCount - 1;
      }
    },
    updateSlidePadesOrder(state, newArray) {
      state.productInfo[state.activeOption].pages.page = newArray;
      state.productInfoModify = true;
    },
    updateModifyStatus(state, isModify) {
      state.productInfoModify = isModify;
    },
    versionIncrement(state, version) {
      state.productInfo.version = version;
    }
  },
  actions: {
    getProductInfoAction(context) {
      context.commit('getProductInfo');
    },
    getPackagesAction(context, vue) {
      context.commit('getPackages', vue);
    },
    getPackageLogsAction(context, vue) {
      context.commit('getPackageLogs', vue);
    }

  }
})


